/**
 * Created by Baggio on 2018-2-6.
 */
$(function () {

    var leftSel = $("#selectL");
    var rightSel = $("#selectR");
    $("#toright").bind("click",function(){
        leftSel.find("option:selected").each(function(){
            $(this).remove().appendTo(rightSel);
        });
    });
    $("#toleft").bind("click",function(){
        rightSel.find("option:selected").each(function(){
            $(this).remove().appendTo(leftSel);

        });
    });

    leftSel.dblclick(function(){
        $(this).find("option:selected").each(function(){
            $(this).remove().appendTo(rightSel);
            getActivityIds();
        });
    });
    rightSel.dblclick(function() {
        $(this).find("option:selected").each(function () {
            $(this).remove().appendTo(leftSel);
            getActivityIds();
        });

    });

        $("#distributorType").change(function () {
        var distributorType = $("#distributorType").val();
        var postData = {distributorType: distributorType};
        $.ajax({
            type: 'POST',
            data: postData,
            url: '/distributor/getSuperiorDistributor.html',
            dataType:'json',
            success: function (data) {
            //    var data=JSON.parse(res)
                if (data.code != 1) {
                    alert(data.msg);
                    return;
                }
                console.log(data);
                var superiorList = data.data;
                console.log(superiorList);
                for (var i = 0; i < superiorList.length; i++) {
                    $("#superiorId").append("<option value='" + superiorList[i].tid + "'>" + superiorList[i].name + "("+superiorList[i].code+")</option>");
                }


            }
        })

    });

    $("#form-member-add").validate({
        rules: {},
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler: function (form) {
            $.ajax({
                cache: true,
                type: 'POST',
                data: $('#form-member-add').serialize(),
                url: '/distributor/modifyDistributor.html',
                //async:false,
                dataType: 'json',
                success: function (data) {
                    if (data.code != 1) {
                        layer.msg(data.msg, {icon: 5, time: 1000});
                        console.log(data.msg);
                        return;
                    }
                    layer.msg('操作成功!', {icon: 6, time: 1000}, function () {
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }
            })
        }
    });
});
function getActivityIds() {

    var activityIds = $("#selectR option").map(function(){
        return $(this).val();
    }).get().join(",")
    $("#activityId").val(activityIds);
}